//
//  NSDate+extension.swift
//  WeiBo
//
//  Created by wukeng on 16/8/31.
//  Copyright © 2016年 wukeng. All rights reserved.
//

import Foundation


extension NSDate{
    class func createDateString(createAtStr : String) -> String {
        //1.创建时间格式化对象
        let fmt = NSDateFormatter()
        fmt.dateFormat = "EEE MM dd HH:mm:ss Z yyyy"
        fmt.locale = NSLocale(localeIdentifier: "en")
        //2.将字符串时间，转成NSDate类型
        guard let createDate = fmt.dateFromString(createAtStr) else{
            return ""
        }
        //创建当前时间
        let nowDate = NSDate()
        //计算创建时间和当前时间的时间差
        let interval =  Int(nowDate.timeIntervalSinceDate(createDate))
        //对时间间隔进行处理
        if interval < 60 {
            return "刚刚"
        }
        if interval < 60*60 {
           return "\(interval / 60)分钟前"
        }
        if interval < 60*60*24 {
           return "\(interval/(60*60))小时前"
        }
        //昨天的日期
        //创建日历对象
        let caledar = NSCalendar.currentCalendar()
        //处理昨天的数据
        if caledar.isDateInYesterday(createDate) {
            fmt.dateFormat = "昨天 HH:mm"
            let timeStr = fmt.stringFromDate(createDate)
            return timeStr
        }
        //一年内的日期
        let cmps = caledar.components(.Year, fromDate: createDate, toDate: nowDate, options: [])
        if cmps.year < 1 {
            fmt.dateFormat = "MM-dd HH:mm"
            let timeStr = fmt.stringFromDate(createDate)
            return timeStr
        }
        fmt.dateFormat = "yyyy-MM-dd HH:mm"
        let timeStr = fmt.stringFromDate(createDate)
        return timeStr
    }
}